<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of welcome
 *
 * @author Obi
 */
class Admin extends MY_Controller {

    function __construct() {
        parent::__construct();

        $this->m_user = new M_user();
    }

    function index() {

        $cek_admin = $this->session->userdata('cek_admin');
        if ($cek_admin)
            redirect("admin_home");

        $data['title'] = 'Data user Pengguna';

        $data['list'] = $this->m_user->get();
        $data['error'] = "";
        $data['isi'] = 'form';
        $this->load->view('template_admin', $data);
    }

    function proses_login() {

        $data['asset'] = array(
        );

        $data['isi'] = 'form';
        $data['title'] = 'Login';

        $username = mysql_real_escape_string($this->input->post('username'));
        $password = md5($this->input->post('password'));

        $jumlah_dt = $this->m_user->where("nama_user = '$username' and password = '$password'")->count();

        if ($jumlah_dt == 1) {
            $gt_dt = $this->m_user->where("nama_user = '$username' and password = '$password'")->get();
            $data = array(
                'nama_user' => $gt_dt->nama_user,
                'id_user' => $gt_dt->id,
                'cek_admin' => TRUE
            );
            $this->session->set_userdata($data);
            redirect('admin_home');
        } else {
            $data['error'] = "User Login dan Password Salah, silahkan dicoba lagi";
            $this->load->view('template_admin', $data);
        }
    }

    function logout() {
        $data = array(
            'nama_user' => '',
            'id_user' => '',
            'cek_admin' => FALSE
        );
        $this->session->unset_userdata($data);
        redirect('admin');
    }

}

?>
